<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/18
 * Time: 16:50
 */

namespace app\admin\controller;

use app\admin\controller\Base;
use app\common\model\ArticleCategory;
use think\facade\Config;
use think\facade\Request;
use app\common\model\Article as ArticleModel;
class Article extends Base
{
    public function index(){
        $data = ArticleModel::all();
        $this->view->assign('data', $data);

        return $this->view->fetch();
    }

    public function doupload(){
//        halt($_FILES);
        echo '{"status":true,"data":{"id":"431cbf5cfe3e45c4abcea878723d7b89"},"message":"操作成功"}';exit;
    }

    public function edit(){
        $catelst = ArticleCategory::all(function($query){
            $query->column('id','name');
        });
        $this->view->assign('catelst', $catelst);
        return $this->view->fetch();
    }

    # 栏目列表
    public function catelst(){
        $catelist = ArticleCategory::all();
        $this->view->assign('catelist', $catelist);
        $this->view->assign('empty', '暂时没有栏目信息');
        return $this->view->fetch();
    }

    # 新增栏目
    public function editcate(){
        $data = null;
        $id = Request::param('id', 0);
        if ($id){
            $data = ArticleCategory::find($id);
        }
        $this->view->assign('data', $data);
        return $this->view->fetch();
    }

    # 保存编辑
    public function doeditcate(){
        $this->checkIsAjax();
        # 数据验证
        $data = Request::param();
        $res = $this->validate($data, 'app\common\validate\ArticleCategory');
        if (true !== $res){
            $this->ckJsonReturn($res);
        }
        if ($data['id']){
            $res = ArticleCategory::update($data);
        }else{
            $res = ArticleCategory::create($data);
        }
        # 保存数据
        if( ! $res){
            $this->ckJsonReturn(Config::get('sys_db_failure_msg'));
        }
        $this->ckJsonReturn('保存成功', 1, ['go_back'=>1]);
    }

    # 删除分类
    public function delcate(){
        $this->checkIsAjax();
        $id = Request::param('id', '');
        if (empty($id)){
            $this->ckJsonReturn();
        }
        # 先判断下这个分类下面有没有文章  todo

        # 删除分类
        ArticleCategory::destroy($id);
        $this->ckJsonReturn('删除成功', 1, null);
    }
}